package com.example.wishstar.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.wishstar.MyApplication;
import com.example.wishstar.R;
import com.example.wishstar.databinding.FragmentUsersBinding;
import com.example.wishstar.model.Users;
import com.example.wishstar.view.CustomOperateDialog;
import com.example.wishstar.view.activity.LoginActivity;
import com.example.wishstar.view.activity.OldActivity;
import com.example.wishstar.view.activity.ResetActivity;
import com.example.wishstar.view.activity.UpdateUserMsgActivity;
import com.example.wishstar.viewmodel.UsersViewModel;
import com.xujiaji.happybubble.BubbleDialog;
import com.xujiaji.happybubble.BubbleLayout;

public class UsersFragment extends Fragment {
    private String TAG = "UsersFragment";
    private FragmentUsersBinding fragmentUserBinding;
    private UsersViewModel usersViewModel;
    private SwipeRefreshLayout swipeRefresh;

    private String nickname;
    private String motto;

    private CustomOperateDialog moreDialog;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        usersViewModel = new ViewModelProvider(this).get(UsersViewModel.class);
        fragmentUserBinding = FragmentUsersBinding.inflate(inflater, container, false);
        View root = fragmentUserBinding.getRoot();

        usersViewModel.getUsersLiveData().observe(getViewLifecycleOwner(), new Observer<Users>() {
            @Override
            public void onChanged(Users users) {
                Log.e(TAG, "onChanged()");
                if(users != null) {
                    fragmentUserBinding.setUsers(users);
                }
            }
        });
        swipeRefresh = fragmentUserBinding.usersSwipeRefresh;
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                usersViewModel.refresh();
                swipeRefresh.setRefreshing(false);
            }
        });

        fragmentUserBinding.linearlayoutChangePassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ResetActivity.class);
                startActivity(intent);
            }
        });

        fragmentUserBinding.linearlayoutOldHabit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), OldActivity.class);
                startActivity(intent);
            }
        });

        TextView title = root.findViewById(R.id.more_title);
        title.setText(getContext().getString(R.string.title_user));

        ImageView moreImage = root.findViewById(R.id.title_more_image);
        BubbleDialog.Position position = BubbleDialog.Position.BOTTOM;
        BubbleLayout bubbleLayout = new BubbleLayout(getContext());
        bubbleLayout.setBubbleColor(getContext().getColor(R.color.alpha_65_black));

        moreImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (moreDialog == null) {
                    moreDialog = new CustomOperateDialog(getContext(), R.layout.dialog_users_more)
                            .setPosition(position)
                            .setClickedView(moreImage)
                            .calBar(true)
                            .setBubbleLayout(bubbleLayout);
                }
                moreDialog.show();
                moreDialog.setClickListener(new CustomOperateDialog.OnClickCustomButtonListener() {
                    @Override
                    public void onClick(String str) {
                        Log.e(TAG, "onClick() ->" +  str);
                        if (str.equals("退出登录")) {
                            usersViewModel.logOut();
                            Intent intent = new Intent(getActivity(), LoginActivity.class);
                            moreDialog.dismiss();
                            startActivity(intent);
                            getActivity().finish();
                        }
                        else if (str.equals("修改个人信息")) {
                            Bundle bundle = new Bundle();
                            bundle.putString("nickname", usersViewModel.getUsersLiveData().getValue().nickname);
                            bundle.putString("motto", usersViewModel.getUsersLiveData().getValue().motto);
                            Intent intent = new Intent(getActivity(), UpdateUserMsgActivity.class);
                            intent.putExtras(bundle);
                            moreDialog.dismiss();
                            startActivity(intent);
                        }
                    }
                });
            }
        });

        return root;
    }

    public void onDestroyView() {
        super.onDestroyView();
        fragmentUserBinding = null;
    }
}
